package com.xl.compute;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class ComputeActivity extends Activity {
	
    private Button button0;
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button button7;
    private Button button8;
    private Button button9;
    private Button button_delete;
    private Button button_back;
    private Button button_add;
    private Button button_minus;
    private Button button_multiply;
    private Button button_divide;
    private Button button_not;
    private Button button_equal;
    private Button button_dot;
    private EditText et;
    
    String str = "0";
    boolean flag = false;//判断加减乘除按钮是否已经按下
	char op;
	double n1 = 0,n2 = 0.0,result = 0;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                
        et = (EditText) findViewById(R.id.text);
        button0 = (Button) findViewById(R.id.button0);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);
        button6 = (Button) findViewById(R.id.button6);
        button7 = (Button) findViewById(R.id.button7);
        button8 = (Button) findViewById(R.id.button8);
        button9 = (Button) findViewById(R.id.button9);
        button_delete = (Button) findViewById(R.id.button_delete);
        button_back = (Button) findViewById(R.id.button_back);
        button_add = (Button) findViewById(R.id.button_add);
        button_minus = (Button) findViewById(R.id.button_minus);
        button_multiply = (Button) findViewById(R.id.button_multiply);
        button_divide = (Button) findViewById(R.id.button_divide);
        button_not = (Button) findViewById(R.id.button_not);
        button_equal = (Button) findViewById(R.id.button_equal);
        button_dot = (Button) findViewById(R.id.button_dot);
        
        button0.setOnClickListener(number_listener);
        button1.setOnClickListener(number_listener);
        button2.setOnClickListener(number_listener);
        button3.setOnClickListener(number_listener);
        button4.setOnClickListener(number_listener);
        button5.setOnClickListener(number_listener);
        button6.setOnClickListener(number_listener);
        button7.setOnClickListener(number_listener);
        button8.setOnClickListener(number_listener);
        button9.setOnClickListener(number_listener);
        button_delete.setOnClickListener(del_listener);
        button_back.setOnClickListener(del_listener);
        button_add.setOnClickListener(cal_listener);
        button_minus.setOnClickListener(cal_listener);
        button_multiply.setOnClickListener(cal_listener);
        button_divide.setOnClickListener(cal_listener);
        button_not.setOnClickListener(dnot_listener);
        button_equal.setOnClickListener(cal_listener);
        button_dot.setOnClickListener(dnot_listener);       
    }
    
    private View.OnClickListener number_listener = new OnClickListener(){

		@Override
		public void onClick(View v){
			// TODO Auto-generated method stub 
			if (flag == true){
				str = "";
				flag = false;
			}
			input(v);			
		}   	
    };
    
    private void input(View v){
    	Button btn = (Button) findViewById(v.getId());
    	String btn_number = btn.getText().toString();
    	if(Integer.valueOf(btn_number)==0){
    		if((Double.valueOf(et.getText().toString())==0)){
        		str = "0";
        		et.setText(str);
    		}
    		else{
    			str += btn_number;
    			et.setText(str);
    		}
    	}
    	else{
    		if((Double.valueOf(et.getText().toString())==0)){
    			str = "";
    		}
    		str += btn_number;
			et.setText(str);
    	}
	}	
    		
    
    private View.OnClickListener del_listener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			// 判断按钮是退格还是全部清除
			switch(v.getId()){
			case R.id.button_back:
				String str_b = et.getText().toString();
				if((str_b.length())>0){
    				str = str_b.substring(0,str.length()-1);
    				et.setText(str);
				}
				break;
			case R.id.button_delete:
				n1 = 0.0;result = 0.0; 
				flag = false;
				str = "0";
				et.setText(str);
				break;			
			}
		}   	
    };
	
    private View.OnClickListener cal_listener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			
			// 判断计算按钮，加、减、乘、除、等于
			switch(v.getId()){
			case R.id.button_add:
				n1 = Double.valueOf(et.getText().toString());
				op = '+';	
				flag = true;
				break;
			case R.id.button_minus:
				n1 = Double.valueOf(et.getText().toString());
				op = '-';
				flag = true;
				break;
			case R.id.button_multiply:
				n1 = Double.valueOf(et.getText().toString());
				op = '*';
				flag = true;
				break;
			case R.id.button_divide:
				n1 = Double.valueOf(et.getText().toString());
				op = '/';
				flag = true;
				break;
			case R.id.button_equal:
				n2 = Double.valueOf(et.getText().toString());
				flag = true;
				calculate();			
				break;
			}
		}		
	};
	
	private void calculate(){
		if(op == '+'){
			result = n1 + n2;
			display();
	//		et.setText(String.valueOf(result));
		}
		else if(op == '-'){
			result = n1 - n2;
			display();
	//		et.setText(String.valueOf(result));
		}
		else if(op == '*'){
			result = n1 * n2;
			display();
		//	et.setText(String.valueOf(result));
		}
		else if(op == '/'){
			if(n2==0){
				et.setText("Infinity");
			}
			else{
				result = n1 / n2;
				display();
			//	et.setText(String.valueOf(result));
			}				
		}
		n1 = 0.0;n2 = 0.0;result = 0.0;
	}
	
	private void display(){
		if(result == Math.round(result)){
			String val = new DecimalFormat("0.##").format(result);
			et.setText(val);
		}
		else{
			NumberFormat f=NumberFormat.getInstance();  //创建一个格式化类f
			  f.setMaximumFractionDigits(13);    //设置小数位的格式
			  String s=f.format(result);          //格式化数据result,将result格式化为小数点后13位
			et.setText(s);	
		}
			
	}
		
	private View.OnClickListener dnot_listener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			// 判断按钮是取反还是点号
			switch(v.getId()){
			case R.id.button_dot:
				if((et.getText().toString()).indexOf(".")!=-1){break;}//如果不是整数
				else{
					str += ".";
					et.setText(str);
					break;
					}
			case R.id.button_not:
				if(str==""||Double.valueOf(et.getText().toString())==0){break;}
				else{
					if(Double.valueOf(et.getText().toString())>0)str = "-"+et.getText();
					else str = et.getText().toString().substring(1,str.length());
					et.setText(str);
    				break; 
				}
			}		
		}
	};
}